<?php
/**
 * Created by PhpStorm.
 * User: jswei
 * Date: 2018/4/24
 * Time: 8:54
 */

namespace app\admin\model;

use think\Model;

class Redpack extends Model{

    protected $autoWriteTimestamp = true;

    /**
     * @param array $where
     * @param int $pageSize
     * @param string $field
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($where= [], $pageSize=10,$field=''){
        $field = $field ? $field: "{$this->getTable()}.*,think_mch.name as mc_name";
        $list = $this
            ->field($field)
            ->join('think_mch',"think_mch.id = {$this->getTable()}.mc_id")
            ->order($this->getTable().'.create_time desc')
            ->where($where)
            ->paginate($pageSize);
        return $list;
    }

    /**
     * 添加申请
     * @param string $name              名称
     * @param string $proxy_id          代理id
     * @param string $mc_id             商户id
     * @param int $total                充值金额
     * @param int $free                 手续费
     * @param string $totals            实收金额
     * @param int $type
     * @param string $out
     * @return $this|bool
     */
    public function addNew($name='',$proxy_id='',$mc_id='',$total=0,$free=0,$totals='',
        $type=1,&$out=''){
        $valid = new \app\admin\validate\Redpack();
        $data = [
            'name'=>$name,
            'proxy_id'=>$proxy_id,
            'mc_id'=>$mc_id,
            'total'=>$total,
            'free'=>$free,
            'totals'=>$totals,
            'type'=>$type,
            'order_id'=>build_order_no()
        ];
        if(!$valid->check($data)){
            $out = $valid->getError();
            return false;
        }
        $out = $data;
        return $this;
    }

    /**
     * 确认充值
     * @param $id
     * @param $status
     * @param $dec
     * @param $config
     * @param null $out
     * @return bool
     * @throws \think\exception\PDOException
     */
    public function updateStatus($id,$status,$dec,$config,&$out=null){
        $info = $this->get($id);
        if(!$info){
            $out= '申请不存在';
            return false;
        }
        if($info['status']!=1){
            $out= '申请已被处理';
            return false;
        }
        $this->startTrans();
        if($status==2){
            $money = $info['free'] * $config['daili'] * (1-0.01);
            $info->status = $status;
            $mch = new Mch();
            $_mch = $mch->get($info['mc_id']);
            $_mch->hb_money = $_mch['hb_money'] + $info['total'];
            $proxy = new Proxy();
            $_proxy = $proxy->get($info['proxy_id']);
            $_proxy->money = $_proxy['money'] + $money;
            $log = new ProxyMoney();
            $result3 = $log
                ->addNew($info['name'],0,$info['mc_id'],$money,2,$info['proxy_id'],$out1)
                ->save();
            $result = $_mch->save();
            $result1 = $info->save();
            $result2 = $_proxy->save();
        }else{
            $info->update_time = time();
            $info->info = $dec;
            $info->status = $status;
            $result1 = $info->save();
            $result = $result2 = $result3 = true;
        }
        if($result && $result1 && $result2 && $result3){
            $this->commit();
            return true;
        }else{
            $this->rollback();
            return false;
        }
    }

    /**
     * 格式时间
     * @param $value
     * @return false|string
     */
    protected function getUpdateTimeAttr($value){
        return  $value?date('Y-m-d H:i:s',$value):'--';
    }

    protected function getInfoAttr($value){
        return  $value?$value:'--';
    }
}